<template>
  <div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
      <template #toolbar>
        <a-button type="primary" @click="initModelVisible = true">重置所有教师任务数据</a-button>
        <a-button v-if="hasPermission('addDept')" type="primary" @click="handleMsg">
          把公告内容推送到微信
        </a-button>
        <a-button v-if="hasPermission('addDept')" type="primary" @click="handleCreate">
          新增设置
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
             label:'编辑',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
             label:'删除',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <Modal @register="registerModal" @success="handleSuccess" />
    <p class="tips">微信通知推送说明：通知对象为全体已完成微信绑定的教师。微信消息推送只有在用户主动点击订阅的情况下才会推送成功。且每次点击订阅只能推送一次。可以通知用户在订阅时勾选长期订阅选项。</p>
    <a-modal v-model:visible="initModelVisible" title="重置任务周期数据" @ok="handleInitNewTaskWeek()">
      <div style="padding: 28px">
        <p>此操作将会重置所有教师的积分、已经提交申报的任务、已经完成的任务、所有的历史用户通知消息。确定要执行吗？</p>
        <p style="color: #999">提示：修改任务周期的时间请前往："内容管理"->"内容设置"->"任务开始时间"、"任务结束时间"</p>
      </div>
    </a-modal>
  </div>
</template>
<script lang="ts">
import {defineComponent, nextTick, ref} from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import {SettingsList, SettingsDel, sendWechatMsgAdminNoticeServer} from '/@/api/article/Settings';
  import { useModal } from '/@/components/Modal';
  import Modal from './Modal.vue';
  import { columns, searchFormSchema } from './schema.data';
  import { usePermission } from '/@/hooks/web/usePermission';
  import {message} from "ant-design-vue";
import {initNewTaskWeek} from "/@/api/taskCenter/taskCenter";
  export default defineComponent({
    name: 'SettingsManagement',
    components: { BasicTable, Modal, TableAction },
    setup() {
      const initModelVisible = ref(false);
      const initModelLoading = ref(false);
      const { hasPermission } = usePermission();
      const [registerModal, { openModal }] = useModal();
      const [registerTable, { reload, expandAll,getDataSource }] = useTable({
        title: '内容设置',
        api: SettingsList,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        isTreeTable: true,
        pagination: false,
        striped: false,
        useSearchForm: false,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        canResize: false,
        actionColumn: {
          width: 150,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
          ifShow: true,
        },
      });

      function handleMsg() {
        const content = getDataSource().find(f=>f.key==='index_radio')?.value
        console.log('content',content)
        if(!content){
          return message.error('发送失败，未读取到公告内容')
        }
        sendWechatMsgAdminNoticeServer().then(res=>{
          console.log('res',res)
          const max  = res.length;
          let count = 0;
          res.map(f=>{
            if(JSON.parse(f.res).errcode === 0){
              count++;
            }
          })
          message.success(`已发送${max}人,成功发送${count}人`);
        })
      }

      function handleInitNewTaskWeek() {
        initModelVisible.value = false
        initNewTaskWeek().then(() => {
          message.success('重置成功')
        })
      }

      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      function handleDelete(record: Recordable) {
        SettingsDel(record.id).then(() => {
          reload();
        });
      }

      function handleSuccess() {
        reload();
      }
      function onFetchSuccess() {
        nextTick(expandAll);
      }
      return {
        initModelVisible,
        initModelLoading,
        registerTable,
        registerModal,
        handleMsg,
        handleCreate,
        handleEdit,
        handleInitNewTaskWeek,
        handleDelete,
        handleSuccess,
        hasPermission,
        onFetchSuccess,
      };
    },
  });
</script>
<style scoped>
.tips{
  padding: 28px;
  font-size: 16px;
  color: #999;
}
</style>
